package plus.H5D3B5016;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import plus.H5D3B5016.bean.Cointrade;
import plus.H5D3B5016.bean.WT;
import plus.H5D3B5016.event.EventType;
import plus.H5D3B5016.event.MyEvent;
import plus.H5D3B5016.event.PriceEvent;
import plus.H5D3B5016.utils.ACache;
import plus.H5D3B5016.view.MyDialog;
import plus.H5D3B5016.view.RecyclerAdapter;
import plus.H5D3B5016.view.TxDialog;

@ContentView(R.layout.activity_xiadan)
public class XiaDanActivity extends BaseActivity {


    @ViewInject(R.id.left)
    ImageView left;
    @ViewInject(R.id.datetext)
    TextView  datetext;
    @ViewInject(R.id.jiage)
    TextView  jiage;

    @ViewInject(R.id.tpzf)
    TextView  tpzf;
    @ViewInject(R.id.zf)
    TextView  zf;

    @ViewInject(R.id.to1)
    Button   to1;
    @ViewInject(R.id.to2)
    Button to2;



    @ViewInject(R.id.but1)
    TextView  but1;
    @ViewInject(R.id.but2)
    TextView  but2;

    @ViewInject(R.id.gg1)
    TextView  gg1;
    @ViewInject(R.id.gg2)
    TextView  gg2;

    @ViewInject(R.id.m1)
    TextView  m1;
    @ViewInject(R.id.m2)
    TextView  m2;
    @ViewInject(R.id.m3)
    TextView  m3;
    @ViewInject(R.id.m4)
    TextView  m4;
    @ViewInject(R.id.m5)
    TextView  m5;
    @ViewInject(R.id.m6)
    TextView  m6;


    @ViewInject(R.id.yj)
    Button   yj;
    @ViewInject(R.id.yjj)
    Button yjj;
    @ViewInject(R.id.kj)
    Button   kj;
    @ViewInject(R.id.kjj)
    Button kjj;
    @ViewInject(R.id.ybf)
    TextView  ybf;
    @ViewInject(R.id.kbf)
    TextView  kbf;
    @ViewInject(R.id.tpy)
    TextView  tpy;
    @ViewInject(R.id.tpk)
    TextView  tpk;

    @ViewInject(R.id.item0)
    TextView     item0;
    @ViewInject(R.id.item1)
    TextView     item1;
    @ViewInject(R.id.item2)
    TextView     item2;
    @ViewInject(R.id.item3)
    TextView     item3;
    @ViewInject(R.id.item4)
    TextView     item4;
    @ViewInject(R.id.item5)
    TextView     item5;
    @ViewInject(R.id.item6)
    TextView     item6;
    @ViewInject(R.id.wtqy)
    LinearLayout wtqy;

    @ViewInject(R.id.recycler_view)
    RecyclerView recycler_view;


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private double myclose;//当前价格
    private double myopen;// 开盘价格
    private Date mydate;// 最新时间

    WT dfwt=null;// 默认托管

    RecyclerAdapter mRecyclerAdapter;

    List<Cointrade> ccList=new ArrayList<>();//持仓


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        try{
            queryBalance();
            //queryCointradehis();

            mRecyclerAdapter = new RecyclerAdapter<Cointrade>(this, ccList, R.layout.item_cc) {
                @Override
                public void convert(RecyclerViewHolder helper, int position, Cointrade item) {

                    helper.setText(R.id.symbolName,item.getSymbolName());
                    helper.setText(R.id.direction,(item.getDirection().equals("buy")?"做多":"做空"));
                    helper.setText(R.id.inTime,""+sdf.format(new Date(item.getInTime())));
                    helper.setText(R.id.inAmount,"本金:"+item.getInAmount());
                    helper.setText(R.id.feeAmount,"手续费:"+item.getFeeAmount());
                    helper.setText(R.id.rate,"杠杆:"+item.getRate()+"X");

                    //helper.setText(R.id.outAmount,""+item.getOutAmount());
                    helper.setText(R.id.outPrice,""+item.getOutPrice());
                    helper.setText(R.id.inPrice,""+item.getInPrice());
                    helper.setText(R.id.outTime,"平仓时间:"+sdf.format(new Date(item.getOutTime())));

                    if(item.getDirection().equals("buy")){
                        ((TextView)helper.getView(R.id.direction)).setTextColor(getResources().getColor(R.color.zd));
                    }else{
                        ((TextView)helper.getView(R.id.direction)).setTextColor(getResources().getColor(R.color.zk));
                    }


                    if((item.getOutAmount()-item.getInAmount())>0){
                        ((TextView)helper.getView(R.id.outAmount)).setTextColor(getResources().getColor(R.color.zd));
                        ((TextView)helper.getView(R.id.outPrice)).setTextColor(getResources().getColor(R.color.zd));
                        helper.setText(R.id.outAmount,""+(item.getOutAmount()-item.getInAmount()));
                    }else if((item.getOutAmount()-item.getInAmount())<0){
                        ((TextView)helper.getView(R.id.outAmount)).setTextColor(getResources().getColor(R.color.zk));
                        ((TextView)helper.getView(R.id.outPrice)).setTextColor(getResources().getColor(R.color.zk));
                        helper.setText(R.id.outAmount,""+(item.getOutAmount()-item.getInAmount()));
                    }else{
                        ((TextView)helper.getView(R.id.outAmount)).setTextColor(getResources().getColor(R.color.zh));
                        ((TextView)helper.getView(R.id.outPrice)).setTextColor(getResources().getColor(R.color.zh));
                        helper.setText(R.id.outAmount,""+(item.getInAmount()));
                    }
                }
            };
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recycler_view.setLayoutManager(layoutManager);
            recycler_view.setAdapter(mRecyclerAdapter);

        }catch (Exception e){}

    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public  void onEvent(PriceEvent priceEvent){
        try{

            String txst=ACache.get(getApplicationContext()).getAsString("txst");
            int st=30;
            if(null!=txst){
                st=Integer.valueOf(txst);
            }

            if(priceEvent.getZf()>0){
                zf.setTextColor(getResources().getColor(R.color.zd));
            }else{
                zf.setTextColor(getResources().getColor(R.color.zk));
            }
            tpzf.setText(st+"秒内涨幅:");
            zf.setText(String.format("%.2f", priceEvent.getZf()));
        }catch (Exception e){}
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public  void onEvent(MyEvent myEvent){
        if(myEvent.getType()!= EventType.READ){
            return;
        }

        JSONObject jo=(JSONObject)myEvent.getObj();
        try {
            //Log.d("xxxx",jo.toString());

            double close=jo.getDouble("close");// 最新价
            double open=jo.getDouble("open");// 今日开盘价格
            myopen=open;

            Long timestamp=jo.getLong("timestamp");// timestamp
            myclose=close;
            mydate=new Date(timestamp);
            //datetext.setText("moon send 时间:"+sdf.format(mydate));
            jiage.setText("BTC/USDT指数:"+close);
            if(myclose>myopen){
                jiage.setTextColor(getResources().getColor(R.color.zd));
            }else{
                jiage.setTextColor(getResources().getColor(R.color.zk));
            }

        }catch (Exception e){}
    }

    private void showCoverDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("关闭窗口将取消限价委托,是否关闭？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();
    }

    /**
     * 限价购买
     */
    private void sendQr(){
        WT wt=new WT();
        wt.setType(0);
        wt.setRate(gg);
        wt.setDirection((fx)?"buy":"sell");
        wt.setDuration(25);
        wt.setInAmount(je);
        wt.setSlPercent(y);
        wt.setTpPercent(k);
        wt.setTxCurrency("btcusdt");
        xiadan(wt);
    }

    @Event(value = {R.id.left,R.id.but1,R.id.but2,
            R.id.m1,R.id.m2,R.id.m3,R.id.m4,R.id.m5,R.id.m6,
            R.id.gg1,R.id.gg2,R.id.to1,R.id.to2,R.id.to3,
            R.id.yj,R.id.yjj,R.id.kj,R.id.kjj},type = View.OnClickListener.class)
    private void onclick(View view){
        switch (view.getId()){
            case R.id.left:
                if(null!=dfwt&&dfwt.getState()==0){
                    showCoverDialog();
                }else{
                    finish();
                }
                break;
            case R.id.but1:
                fx=true;
                fx();
                break;
            case R.id.but2:
                fx=false;
                fx();
                break;
            case R.id.m1:
                je=100;
                ldje();
                break;
            case R.id.m2:
                je=500;
                ldje();
                break;
            case R.id.m3:
                je=1000;
                ldje();
                break;
            case R.id.m4:
                je=2000;
                ldje();
                break;
            case R.id.m5:
                je=5000;
                ldje();
                break;
            case R.id.m6:
                je=10000;
                ldje();
                break;
            case R.id.gg1:
                gg=25;
                ldgg();
                break;
            case R.id.gg2:
                gg=50;
                ldgg();
                break;
            case R.id.to1:
                to=0;
                ldto();
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示");
                builder.setMessage("确认购买");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        sendQr();
                    }
                });
                builder.setNegativeButton("取消",null);
                builder.show();
                break;
            case R.id.to2:
                to=1;
                ldto();
                MyDialog adDialog=new MyDialog(XiaDanActivity.this,fx,(int)myclose);
                adDialog.show(getSupportFragmentManager(),"ad");
                adDialog.setOnConfirmClickListener(new MyDialog.OnConfirmClickListener() {
                    @Override
                    public void onConfirm(int st, int et) {
                        //委托
                        dfwt=new WT();
                        dfwt.setType(1);
                        dfwt.setRate(gg);
                        dfwt.setDirection((fx)?"buy":"sell");
                        dfwt.setDuration(25);
                        dfwt.setInAmount(je);
                        dfwt.setSlPercent(y);
                        dfwt.setTpPercent(k);
                        dfwt.setMin(st);
                        dfwt.setMax(et);
                        dfwt.setTxCurrency("btcusdt");
                        setWTUI(dfwt);
                        wtqy.setVisibility(View.VISIBLE);

                        mHandler.postDelayed(runnable, 1000);
                    }
                });

                break;
            case R.id.to3:
                TxDialog txDialog=new TxDialog(XiaDanActivity.this);
                txDialog.show(getSupportFragmentManager(),"tx");
                txDialog.setOnConfirmClickListener(new TxDialog.OnConfirmClickListener() {
                    @Override
                    public void onConfirm(int st, int et) {
                        ACache.get(XiaDanActivity.this).put("txst",""+st);
                        ACache.get(XiaDanActivity.this).put("txet",""+et);
                    }
                });

                break;
            case R.id.yj:
                ldbfby(-10);break;
            case R.id.yjj:
                ldbfby(10);break;
            case R.id.kj:
                ldbfbk(-10);break;
            case R.id.kjj:
                ldbfbk(10);
                break;

        }
    }

    private void setWTUI(WT wt){
        if(null==wt){return;}
        item0.setText(""+wt.getInAmount());
        item1.setText(""+wt.getMin()+"-"+wt.getMax());
        item2.setText(""+wt.getRate()+"X");
        item3.setText(""+wt.getSlPercent());
        item4.setText(""+wt.getTpPercent());


        String state="";
        if(wt.getState()==0){
            state="等待下单";
        }else if(wt.getState()==1){
            state="下单成功";
        }else if(wt.getState()==2){
            state="下单失败";
        }
        item5.setText(state);
        item6.setText("取消");
        item6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dfwt=null;
                wtqy.setVisibility(View.INVISIBLE);
            }
        });


    }


    int y=100;
    int k=100;
    /**
     * 赢
     * 联动百分比
     */
    private void ldbfby(int into){
        y=y+into;
        if(into<0){
            if(y<10){
                y=10;
            }
        }else{
            if(y>100){
                y=100;
            }
        }

        ybf.setText(""+y);
        try{
            tpy.setText("止盈（盈利 "+(je*y/100)+"）");
        }catch (Exception e){}
    }
    /**
     * 亏
     * 联动百分比
     */
    private void ldbfbk(int into){
        k=k+into;
        if(into<0){
            if(k<10){
                k=10;
            }
        }else{
            if(k>100){
                k=100;
            }
        }
        kbf.setText(""+k);

        try{
            tpk.setText("止损（亏损 "+(je*k/100)+"）");
        }catch (Exception e){}
    }

    boolean fx=true;
    /**
     * 做多
     */
    private void fx(){

        if(fx){
            but1.setBackground(getResources().getDrawable(R.drawable.ic_zd));
            but2.setBackground(getResources().getDrawable(R.drawable.ic_zh));
            but1.setTextColor(Color.WHITE);
            but2.setTextColor(Color.parseColor("#283043"));

        }else{
            but1.setBackground(getResources().getDrawable(R.drawable.ic_zh));
            but2.setBackground(getResources().getDrawable(R.drawable.ic_zk));
            but1.setTextColor(Color.parseColor("#283043"));
            but2.setTextColor(Color.WHITE);

        }

        ldje();
        ldgg();
        ldto();
    }




    int je=100;

    /**
     * 联动金额
     */
    private void ldje(){
        int fxres=0;
        if(fx){
            fxres=R.drawable.ic_zd;
        }else{
            fxres=R.drawable.ic_zk;
        }

        switch (je){
            case 100:
                m1.setBackground(getResources().getDrawable(fxres));
                m2.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m3.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m4.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m5.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m6.setBackground(getResources().getDrawable(R.drawable.ic_zh));



                m1.setTextColor(Color.parseColor("#283043"));
                m2.setTextColor(Color.parseColor("#283043"));
                m3.setTextColor(Color.parseColor("#283043"));
                m4.setTextColor(Color.parseColor("#283043"));
                m5.setTextColor(Color.parseColor("#283043"));
                m6.setTextColor(Color.parseColor("#283043"));
                m1.setTextColor(Color.WHITE);

                break;
            case 500:
                m1.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m2.setBackground(getResources().getDrawable(fxres));
                m3.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m4.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m5.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m6.setBackground(getResources().getDrawable(R.drawable.ic_zh));

                m1.setTextColor(Color.parseColor("#283043"));
                m2.setTextColor(Color.parseColor("#283043"));
                m3.setTextColor(Color.parseColor("#283043"));
                m4.setTextColor(Color.parseColor("#283043"));
                m5.setTextColor(Color.parseColor("#283043"));
                m6.setTextColor(Color.parseColor("#283043"));
                m2.setTextColor(Color.WHITE);
                break;
            case 1000:
                m1.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m2.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m3.setBackground(getResources().getDrawable(fxres));
                m4.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m5.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m6.setBackground(getResources().getDrawable(R.drawable.ic_zh));

                m1.setTextColor(Color.parseColor("#283043"));
                m2.setTextColor(Color.parseColor("#283043"));
                m3.setTextColor(Color.parseColor("#283043"));
                m4.setTextColor(Color.parseColor("#283043"));
                m5.setTextColor(Color.parseColor("#283043"));
                m6.setTextColor(Color.parseColor("#283043"));
                m3.setTextColor(Color.WHITE);

                break;
            case 2000:
                m1.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m2.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m3.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m4.setBackground(getResources().getDrawable(fxres));
                m5.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m6.setBackground(getResources().getDrawable(R.drawable.ic_zh));

                m1.setTextColor(Color.parseColor("#283043"));
                m2.setTextColor(Color.parseColor("#283043"));
                m3.setTextColor(Color.parseColor("#283043"));
                m4.setTextColor(Color.parseColor("#283043"));
                m5.setTextColor(Color.parseColor("#283043"));
                m6.setTextColor(Color.parseColor("#283043"));
                m4.setTextColor(Color.WHITE);
                break;
            case 5000:
                m1.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m2.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m3.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m4.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m5.setBackground(getResources().getDrawable(fxres));
                m6.setBackground(getResources().getDrawable(R.drawable.ic_zh));

                m1.setTextColor(Color.parseColor("#283043"));
                m2.setTextColor(Color.parseColor("#283043"));
                m3.setTextColor(Color.parseColor("#283043"));
                m4.setTextColor(Color.parseColor("#283043"));
                m5.setTextColor(Color.parseColor("#283043"));
                m6.setTextColor(Color.parseColor("#283043"));
                m5.setTextColor(Color.WHITE);

                break;
            case 10000:
                m1.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m2.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m3.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m4.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m5.setBackground(getResources().getDrawable(R.drawable.ic_zh));
                m6.setBackground(getResources().getDrawable(fxres));

                m1.setTextColor(Color.parseColor("#283043"));
                m2.setTextColor(Color.parseColor("#283043"));
                m3.setTextColor(Color.parseColor("#283043"));
                m4.setTextColor(Color.parseColor("#283043"));
                m5.setTextColor(Color.parseColor("#283043"));
                m6.setTextColor(Color.parseColor("#283043"));
                m6.setTextColor(Color.WHITE);
                break;
        }

        ldbfby(0);
        ldbfbk(0);
    }

    int gg=50;
    private void ldgg(){
        int fxres=0;
        if(fx){
            fxres=R.drawable.ic_zd;
        }else{
            fxres=R.drawable.ic_zk;
        }

        if(gg==50){
            gg2.setBackground(getResources().getDrawable(fxres));
            gg1.setBackground(getResources().getDrawable(R.drawable.ic_zh));
            gg2.setTextColor(Color.WHITE);
            gg1.setTextColor(Color.parseColor("#283043"));
        }else{
            gg2.setBackground(getResources().getDrawable(R.drawable.ic_zh));
            gg1.setBackground(getResources().getDrawable(fxres));
            gg2.setTextColor(Color.parseColor("#283043"));
            gg1.setTextColor(Color.WHITE);
        }

    }


    int to=0;
    private void ldto() {
        int fxres=0;
        if(fx){
            fxres=R.drawable.ic_zd;
        }else{
            fxres=R.drawable.ic_zk;
        }

        if(to==0){
            to1.setBackground(getResources().getDrawable(fxres));
            to2.setBackground(getResources().getDrawable(R.drawable.ic_zh));
            to2.setTextColor(Color.parseColor("#283043"));
            to1.setTextColor(Color.WHITE);
        }else{
            to1.setBackground(getResources().getDrawable(R.drawable.ic_zh));
            to2.setBackground(getResources().getDrawable(fxres));
            to1.setTextColor(Color.parseColor("#283043"));
            to2.setTextColor(Color.WHITE);
        }
    }


    boolean xdz=false;// 是否下单中
    /**
     * 立即下单
     */
    private synchronized void xiadan(final WT wt){
        try {
            if(xdz){return;}
            xdz=true;

            String token=ACache.get(XiaDanActivity.this).getAsString("token");
            if(null==token){
                show("正在检测登录,请稍后再试");return;
            }

            loading.show();
            Request request = new Request.Builder()
                    .url(Const.BASE_URL + "/coingroup/cointrade")
                    .addHeader("accesstoken",token)
                    .post(RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),JSON.toJSONString(wt)))
                    .build();


            OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .readTimeout(3, TimeUnit.SECONDS)
                    .build();

            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            wt.setState(2);
                            if(loading.isShowing()){
                                loading.dismiss();
                            }

                            show("网络错误");
                        }
                    });
                }


                @Override
                public void onResponse(Call call,final Response response) throws IOException {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {

                                if(loading.isShowing()){
                                    loading.dismiss();
                                }

                                HashMap map=JSON.parseObject(response.body().string().trim(), HashMap.class);
                                if(response.code()==200){
                                    wt.setState(1);
                                    show("操作成功");
                                    queryBalance();
                                    if(map.containsKey("tradeId")){

                                    }

                                }else{
                                    wt.setState(2);
                                    show(""+map.get("message").toString());
                                }

                                if(wt.getType()==1){
                                    setWTUI(wt);
                                }
                            }catch (Exception e){}
                        }
                    });


                }
            });

        }catch (Exception e){

        }finally {
            xdz=false;
        }
    }




    /**
     * 查询余额
     */
    private  void queryBalance(){
        try {

            String token=ACache.get(XiaDanActivity.this).getAsString("token");
            if(null==token){
                show("正在检测登录,请稍后再试");return;
            }

            loading.show();
            Request request = new Request.Builder()
                    .url(Const.BASE_URL + "/coingroup/coinwallet/current")
                    .addHeader("accesstoken",token)
                    .get()
                    .build();
            OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .readTimeout(3, TimeUnit.SECONDS)
                    .build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call,final IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(loading.isShowing()){
                                loading.dismiss();
                            }

                            show("网络错误"+e.getMessage());
                        }
                    });
                }


                @Override
                public void onResponse(Call call,final Response response) throws IOException {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if(loading.isShowing()){
                                    loading.dismiss();
                                }

                                HashMap map=JSON.parseObject(response.body().string().trim(), HashMap.class);;

                                if(response.code()!=200){
                                    show(""+map.get("message").toString());return;
                                }

                                if(map.containsKey("balance")){
                                    BigDecimal yue=(BigDecimal)map.get("balance");
                                    datetext.setText("账户权益:"+String.format("%.2f",yue));
                                    //datetext.setText("账户权益:7400.39");
                                }

                            }catch (Exception e){
                                Log.d("xxx",e.getMessage());
                            }
                        }
                    });
                }
            });

        }catch (Exception e){
        }
    }



    /**
     * 查询持仓
     */
    private  void queryCointradehis(){
        try {

            String token=ACache.get(XiaDanActivity.this).getAsString("token");
            if(null==token){
                show("正在检测登录,请稍后再试");return;
            }
            ccList.clear();
            loading.show();
            Request request = new Request.Builder()
                    .url(Const.BASE_URL + "/coingroup/cointradehis/page?pageSize=20&pageNum=1")
                    .addHeader("accesstoken",token)
                    .get()
                    .build();
            OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .readTimeout(3, TimeUnit.SECONDS)
                    .build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call,final IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(loading.isShowing()){
                                loading.dismiss();
                            }
                            show("网络错误"+e.getMessage());
                        }
                    });
                }


                @Override
                public void onResponse(Call call,final Response response) throws IOException {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if(loading.isShowing()){
                                    loading.dismiss();
                                }
                                if(response.code()!=200){
                                    return;
                                }
                                JSONObject myjo=new JSONObject(response.body().string().trim());
                                JSONArray ja=myjo.getJSONArray("list");
                                JSONObject jo;
                                for (int i=0;i<ja.length();i++){
                                    jo=ja.getJSONObject(i);
                                    ccList.add(JSON.parseObject(jo.toString(),Cointrade.class));
                                }
                                mRecyclerAdapter.notifyDataSetChanged();

                            }catch (Exception e){
                                Log.d("xxx","出错啦:"+e.getMessage());
                            }
                        }
                    });
                }
            });

        }catch (Exception e){
        }
    }

    /**
     * 处理限价委托
     */
    private Handler mHandler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if(null==dfwt){return;}
            if(myclose>=dfwt.getMin()&&myclose<=dfwt.getMax()){
                xiadan(dfwt);
                return;
            }
           mHandler.postDelayed(this, 1000);
        }
    };


}
